package com.test.mongo;

import cn.hutool.core.bean.BeanUtil;
import com.test.db.api.UserDao;
import com.test.db.api.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnProperty(name = "common.db", havingValue = "mongo")
public class MGUserDAO implements UserDao {

    @Autowired
    private MongoTemplate template;

    @Override
    public User getInfo() {
        MGUser user = template.findOne(Query.query(Criteria.where("name").is("mongo")), MGUser.class);
        return BeanUtil.toBean(user, User.class);
    }
}
